package org.lecric.wind44.client.widget;

import java.util.List;

import org.lecric.wind44.client.model.Spot;

import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;

public class Map44
{
	private final MapWidget mapWidget;

	public Map44(List<Spot> spots)
	{
		mapWidget = new MapWidget(LatLng.newInstance(47.762781,-1.609933), 8);
	    mapWidget.setSize("100%", "100%");
	    mapWidget.setContinuousZoom(true);
	    mapWidget.setScrollWheelZoomEnabled(true);
	    
	    // Add some controls for the zoom level
	    mapWidget.addControl(new LargeMapControl());

	    // Add a marker
	    for(final Spot spot: spots)
	    {
	    	if(spot.getLatitude() != null && spot.getLongitude() != null)
	    	{
	    		// Mazerolles: 47.362781,-1.509933
	    		// PM: 47.449393,-2.463169
	    		// La tranche: 46.343313,-1.437836
	    		// .o: 47.226534,-2.172632
	    		final Marker mazerollesMarker = new Marker(LatLng.newInstance(spot.getLatitude(), spot.getLongitude()));
	    		mazerollesMarker.addMarkerClickHandler(new MarkerClickHandler(){
	    			@Override
	    			public void onClick(MarkerClickEvent event) {
	    				mapWidget.getInfoWindow().open(mazerollesMarker, new InfoWindowContent(spot.getName()));
	    			}
	    		});
	    		mapWidget.addOverlay(mazerollesMarker);
	    	}
	    }
	}

	public MapWidget getMapWidget() {
		return mapWidget;
	}
}
